/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.dictmap.DeleteDict;
import cn.stylefeng.guns.core.common.constant.dictmap.DeptDict;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.system.entity.Customer;
import cn.stylefeng.guns.modular.system.entity.Dept;
import cn.stylefeng.guns.modular.system.entity.Notice;
import cn.stylefeng.guns.modular.system.model.params.DictParam;
import cn.stylefeng.guns.modular.system.service.CustomerService;
import cn.stylefeng.guns.modular.system.warpper.LogWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * 客户管理的控制器
 *
 * @author zxs
 * @Date 2019年6月24日 14:01:36
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    private static String PREFIX = "/modular/system/customer/";

    @Autowired
    private CustomerService customerService;

    /**
     * 跳转到客户管理的首页
     *
     * @author zxs
     * @Date 2019/06/24 2:04 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "customer.html";
    }

    /**
     * 查询客户列表
     *
     * @author zxs
     * @Date 2019/06/24 15:18 PM
     */
    @RequestMapping("/list")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object list(@RequestParam(required = false) String beginTime,
                       @RequestParam(required = false) String endTime,
                       @RequestParam(required = false) String name) {

        //获取分页参数
        Page page = LayuiPageFactory.defaultPage();

        //根据条件查询日志
        List<Map<String, Object>> result = customerService.getCustomer(page, beginTime, endTime, name);
        page.setRecords(new LogWrapper(result).wrap());

        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 跳转到添加通知
     *
     * @author zxs
     * @Date 2019/06/24 4:07 PM
     */
    @RequestMapping("/customer_add")
    public String noticeAdd() {
        return PREFIX + "add.html";
    }

    /**
     * 新增客户
     *
     * @author zxs
     * @Date 22019/06/24 4:07 PM
     */
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public ResponseData add(Customer customer) {
        this.customerService.save(customer);
        return ResponseData.success();
    }

    /**
     * 跳转到客户
     *
     * @author zxs
     * @Date 2019/6/25 4:56 PM
     */
    @Permission
    @RequestMapping("/customer_update")
    public String deptUpdate(@RequestParam("customerId") Long customerId, Model model) {
        Customer customer = this.customerService.getById(customerId);
        model.addAttribute("customer",customer);
        LogObjectHolder.me().set(customer);
        return PREFIX + "customer_edit.html";
    }


    /**
     * 跳转到修改客户
     *
     * @author zxs
     * @Date 2018/6/25 4:56 PM
     */
    @Permission
    @RequestMapping("/customer_detail")
    public String customerDetail(@RequestParam("customerId") Long customerId, Model model) {
        Customer customer = this.customerService.getById(customerId);
        model.addAttribute("customer",customer);
        LogObjectHolder.me().set(customer);
        return PREFIX + "customer_detail.html";
    }

    /**
     * 修改客户信息
     *
     * @author zxs
     * @Date 22019/06/24 4:07 PM
     */
    @RequestMapping(value = "/edit")
    @Permission
    @ResponseBody
    public ResponseData update(Customer customer) {
        this.customerService.updateById(customer);
        return ResponseData.success();
    }



    /**
     * 删除客户接口
     *
     * @author zxs
     * @Date 2019-06-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    @BussinessLog(value = "删除通知", key = "customerId", dict = DeleteDict.class)
    public ResponseData delete(@RequestParam Long customerId) {
        //缓存通知名称
        LogObjectHolder.me().set(ConstantFactory.me().getNoticeTitle(customerId));

        this.customerService.removeById(customerId);
        return ResponseData.success();
    }
}
